#include "Enemy.h"
#include "Pen.h"
#include "Audio.h"

Enemy::Enemy()
{
	Init();
}

Enemy::Enemy(D3DXVECTOR3 Pos)
{
	m_vPosition = Pos;
	Init();
}

Enemy::Enemy(float x,float y,float z)
{
	m_vPosition = D3DXVECTOR3(x,y,z);
	Init();
}

void Enemy::Init()
{
	m_fHealth = 10;
	m_fGold = 0;
	m_fDamage = .5f;
	m_eState = AI_IDLE_STATE;
	m_iDecision = 0;
	m_fSpeedMod = 1;
	m_eDir = AI_DIRECTIONS_SIZE;
	m_stRotation.Set(90,0,0);
	m_BoundingBox.top		= (LONG)(m_vPosition.y + 3);
	m_BoundingBox.left		= (LONG)(m_vPosition.x + 3);
	m_BoundingBox.bottom	= (LONG)(m_vPosition.y - 3);
	m_BoundingBox.right		= (LONG)(m_vPosition.x - 3);
	m_Mesh = Pen::Engine()->getMeshByIndex(ENEMY_MESH);
	m_pTexture = Pen::Engine()->getTextureByIndex(RED_TEXTURE);
	m_Type = ENEMY_TYPE;
	m_fScale = 1.0f;
	CameraMan.EyePos = GetCameraPosition();
	CameraMan.LookAt = GetPosition();
}

//Can only receive damage messages
void Enemy::TranslateMessages()
{
	Messenger::Engine()->ReceiveMessages(this);
	if(m_Messages.getSize() != 0)
	{
		for(int i = 0;i < m_Messages.getSize();++i)
		{
			switch(m_Messages.get(i)->MessageType)
			{
			case DAMAGE:
				m_fHealth -= *m_Messages.get(i)->Info;
				Messenger::Engine()->ClearMessage(m_Messages.get(i));
				break;
			default:
				break;
			}
		}
		m_Messages.Clear();
	}
}

bool Enemy::Collided(DrawList* drawables,D3DXVECTOR3 NewPos)
{
	this;
	int TotalDis = 0;
	D3DXVECTOR3 ChangeinPos = NewPos - m_vPosition;
	//If it isnt able to clip it skips this whole function
	if(CLIP)
	{
		for(int i = 0;i < drawables->getSize();++i)
		{
			if(m_iID != drawables->get(i)->m_iID)
			{
				/*//Skips if the distance is to great
				TotalDis = (int)(drawables->get(i)->m_vPosition.x - NewPos.x + drawables->get(i)->m_vPosition.y - NewPos.y);
				if(TotalDis <= 50
					&&*/ if((ChangeinPos.x + this->m_BoundingBox.right < drawables->get(i)->m_BoundingBox.left)
					&& (ChangeinPos.x + this->m_BoundingBox.left > drawables->get(i)->m_BoundingBox.right)
					&& (ChangeinPos.y + this->m_BoundingBox.bottom < drawables->get(i)->m_BoundingBox.top)
					&& (ChangeinPos.y + this->m_BoundingBox.top > drawables->get(i)->m_BoundingBox.bottom))
				{
					switch(drawables->get(i)->m_Type)
					{
					case PLAYER_TYPE:
						Messenger::Engine()->SendMessageA(DAMAGE,m_fDamage,drawables->get(i)->m_iID);
						//Speaker::Engine()->PlaySoundFXFile(ENEMY_ATTACK_SOUND);
					default:
						return CLIP;
					}
					
				}
			}
		}
	}
	return false;
}

bool Enemy::AIUpdate(void* Data)
{
	//Target
	Character* ToAttack = (Character*)Data;
	//Random direction until in range
	if(m_eState == AI_IDLE_STATE)
	{
		
		if(m_iDecision <= 0)
		{
			m_iDecision = 100;
			m_eDir = (eAIDirections)(rand() % AI_DIRECTIONS_SIZE);
		}
		int TotalDis =  (int)(m_vPosition.x - ToAttack->m_vPosition.x) +
						(int)(m_vPosition.y - ToAttack->m_vPosition.y);
		if(TotalDis <= 20)
			m_eState = AI_ATTACK_STATE;
		m_iDecision--;
	}
	//If its not attacking no need to calculate
	if(m_eState == AI_ATTACK_STATE)
	{
		int TotalDis =  (int)(m_vPosition.x - ToAttack->m_vPosition.x) +
						(int)(m_vPosition.y - ToAttack->m_vPosition.y);
		if(TotalDis >= 30)
		{
			m_eState = AI_IDLE_STATE;
			return false;
		}

		if(m_iDecision != 0)m_iDecision = 0;
		D3DXVECTOR3 Me(0,0,0), Him(0,0,0);
		Me = this->m_vPosition;
		Him = ToAttack->m_vPosition;

		//Figures out which direction to go

		//Up and Down
		if(Me.x - .5f <  Him.x && Me.x + .5f > Him.x)
		{
			if(Me.y < Him.y)
			{
				m_eDir = AI_UP_DIR;
				return true;
			}
			else if(Me.y > Him.y)
			{
				m_eDir = AI_DOWN_DIR;
				return true;
			}
		}

		//Left and Right
		else if(Me.y - .5f <  Him.y && Me.y + .5f > Him.y)
		{
			if(Me.x > Him.x)
			{
				m_eDir = AI_RIGHT_DIR;
				return true;
			}
			else if(Me.x < Him.x)
			{
				m_eDir = AI_LEFT_DIR;
				return true;
			}
		}

		//Dig Up left and right
		else if(Me.y < Him.y)
		{
			if(Me.x > Him.x)
			{
				m_eDir = AI_DIGRGTUP_DIR;
				return true;
			}
			else if(Me.x < Him.x)
			{
				m_eDir = AI_DIGLFTUP_DIR;
				return true;
			}
		}

		//Dig Down left and right
		else if(Me.y > Him.y)
		{
			if(Me.x > Him.x)
			{
				m_eDir = AI_DIGRGTDW_DIR;
				return true;
			}
			else if(Me.x < Him.x)
			{
				m_eDir = AI_DIGLFTDW_DIR;
				return true;
			}
		}
	}
	return false;
}

void* Enemy::Update(void* Data)
{
	AIUpdate(Data);
	//Move bounding box to position
	UpdateBox();
	TranslateMessages();
	return 0;
}